package com.lagou.handler;

import com.lagou.service.RpcRequest;
import com.lagou.service.SpringContext;
import com.lagou.service.UserService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class UserServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        // 判断是否符合约定，符合则调用本地方法，返回数据
        RpcRequest request= (RpcRequest) msg;
        UserService userService = (UserService) SpringContext.getBean(Class.forName(request.getClassName()));

        Method[] methods = userService.getClass().getMethods();
        Method method1=null;
        for (Method method:methods){
            if(request.getMethodName().equals(method.getName())){
                method1=method;
            }
        }
        Object result = method1.invoke(userService, request.getParameters());

        ctx.writeAndFlush(result);


    }
}
